package cn.tianyigps.tianyigpsbatterylockalarm.customer;

import cn.tianyigps.tianyigpsbatterylockalarm.checkDetail.BatteryCheckDetail;
import cn.tianyigps.tianyigpsbatterylockalarm.hanlder.BatteryRuleHandler;
import cn.tianyigps.tianyigpsbatterylockalarm.hanlder.BatteryRuleUtil;
import cn.tianyigps.tianyigpskafka.task.bean.Task;
import cn.tianyigps.tianyigpskafka.task.customer.Customer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;


/**
 * @Author: WEI.YUAN
 * @Descirption:电频车锁车报警消费者
 * @Date: 2019/4/15_10:55
 */
@Component
public class BatteryAlarmCustomer extends Customer {

    @Autowired
    BatteryRuleHandler batteryRuleHandler;
    @Autowired
    BatteryCheckDetail batteryCheckDetail;
    @Autowired
    BatteryRuleUtil batteryRuleUtil;


    @Override
    public boolean checkTask(Task task) {
        //params不能为空
        if (CollectionUtils.isEmpty(task.getParams())) {
            return false;
        }
        //task的TaskType不能为空
        if (StringUtils.isEmpty(task.getTaskType())) {
            return false;
        }
        return true;
    }

    @Override
    public void consume(Task task) {
        //php接受规则
        if (task.getNextTask() != null) {
            if (batteryCheckDetail.judgeBattery(task)) {
                if (String.valueOf(task.getParams().get("flag")).equals("true")) {
                    //falg为true存规则
                    batteryRuleUtil.addBatterRule(task);
                } else {
                    //flag为false删除规则
                    batteryRuleUtil.delBatterRule(task);
                }
            }
        } else {
            //处理数据是否报警
            if (batteryCheckDetail.batteryAlarm(task)) {
                batteryRuleHandler.BatteryAlarm(task);
            }
        }


    }
}
